package org.boge.sharpdoc.controller;

import com.github.pagehelper.PageInfo;
import org.boge.sharpdoc.bean.Comment;
import org.boge.sharpdoc.bean.Customer;
import org.boge.sharpdoc.bean.Document;
import org.boge.sharpdoc.bean.LayuiReturnData;
import org.boge.sharpdoc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("commitComment")
    @ResponseBody
    public LayuiReturnData commitComment(Comment comment) {
        commentService.commitComment(comment);
        return new LayuiReturnData(0, true, "评论发布成功，刷新可见。");
    }

    @PostMapping("admin/getAllComment")
    @ResponseBody
    public LayuiReturnData getAllComment(Integer index, Integer pageSize) {
        PageInfo<Comment> pageInfo = commentService.getAllWithPage(index, pageSize);
        List<Comment> cls = pageInfo.getList();
        for (Comment d : cls) {
            Customer customer = new Customer();
            customer.setPhone(d.getCustomer().getPhone());
            Document document = new Document();
            document.setDocid(d.getDocument().getDocid());

            d.setCustomer(customer);
            d.setDocument(document);
        }
        return new LayuiReturnData(0, true, "数据查询成功！", (int) pageInfo.getTotal(), cls);
    }

    @GetMapping("admin/commentPage")
    public String commentPage() {
        return "admin/comment";
    }
}